<?php

namespace App\Kuafu\CustomerCoupon\Controller\Merchant;

use App\Exceptions\BaseException;
use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\CouponExt\Enum\CouponExtEnum;
use App\Kuafu\CustomerCoupon\Service\CustomerCouponService;
use App\Kuafu\Order\Service\OrderPlacingService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CustomerCouponController extends BaseController
{
    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     */
    public function usedList(Request $request): JsonResponse
    {
        $page             = $request->get("page", 1);
        $pageSize         = $request->get("pageSize", 10);
        $shopId           = $request->header("shopId", -1);
        $couponCode       = $request->get("couponCode", "");
        $operateTimeStart = (int)$request->get("operateTimeStart", 0);
        $operateTimeEnd   = (int)$request->get("operateTimeEnd", 0);
        $type             = (int)$request->get('type', 0);
        $serialNum        = (int)$request->get('serialNum', 0);
        $couponFrom       = (int)$request->get('couponFrom', 0);
        $couponTitle      = $request->get('couponTitle', '');
        $orderId          = (string)$request->get('orderId', '');
        return $this->success(
            CustomerCouponService::usedList($page, $pageSize, $shopId, $couponCode, $operateTimeStart, $operateTimeEnd,
                $type, $serialNum, $couponTitle, $orderId, $couponFrom)
        );
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     */
    public function use(Request $request): JsonResponse
    {
        $couponCode = $request->post("couponCode", "");
        $shopId     = $request->header("shopId", -1);
        CustomerCouponService::use($couponCode, $shopId);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     * @throws GuzzleException
     */
    public function checkCoupon(Request $request): JsonResponse
    {
        $couponCode   = $request->get("couponCode", "");
        $couponType   = $request->get('couponType', CouponExtEnum::EXT_PLATFORM_KUAFU);
        $couponNum    = $request->get('couponNum', 1);
        $isPrint      = (bool)$request->get('isPrint', false);
        $orderPlacing = new OrderPlacingService($this->brandId, $this->shopId);

        $checkResult  = $orderPlacing->genOrderByCheckCoupon($couponType, $couponCode, $couponNum, $isPrint);
        return $this->json($checkResult);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     */
    public function cancelUse(Request $request): JsonResponse
    {
        $couponCode = $request->post("couponCode", "");
        $shopId     = $request->header("shopId", -1);
        CustomerCouponService::cancelUse($couponCode, $shopId);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     */
    public function detail(Request $request): JsonResponse
    {
        $couponCode = $request->post("couponCode", "");
        $shopId     = $request->header("shopId", -1);
        return $this->success(CustomerCouponService::merchantCustomerCouponDetail($couponCode, $shopId));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     * @throws GuzzleException
     */
    public function detailInfo(Request $request): JsonResponse
    {
        $couponCode = $request->get("couponCode", "");
        $couponType = $request->get('couponType', CouponExtEnum::EXT_PLATFORM_KUAFU);
        return $this->success(CustomerCouponService::getAllTypeCouponWithVerify($this->shopId, $couponCode,
            $couponType, true));
    }
}
